#!/bin/bash

system_name=`uname -a 2>/dev/null`
if [[ ${system_name} =~ "Ubuntu" ]];then
    echo ${system_name}
else
    echo "Required Ubuntu, aborting!"
    exit 1
fi

# =====================Appium Client================================================
echo ========================Appium Client: python===============================
cmd=`python --version 2>/dev/null`
if [ $? -eq 0 ];then
    echo "Python already exist: "$cmd
else
    echo Install Python...
    cmd=`echo Y | sudo apt install python3`
    if [ $? -ne 0 ];then
        echo FATAL error: Install Python failed.
        exit 1
    fi
    cmd=`sudo  ln -s /bin/python3 /usr/local/bin/python`
    if [ $? -eq 0 ];then
        echo Install Python success, python in /usr/local/bin/python
    fi
fi

# ===================================Install pip=====================================
cmd=`pip --version 2>/dev/null`
if [ $? -ne 0 ];then
    echo Install pip...
    cmd=`echo Y | sudo apt install python3-pip 2>/dev/null`
    if [ $? -ne 0 ];then
        echo FATAL error: Install pip failed.
        exit 1
    fi
    echo Install pip success
fi


# ========================Create Python vevn===============================
cmd=`virtualenv --version 2>/dev/null`
if [ $? -ne 0 ];then
    cmd=`pip install virtualenv 2>/dev/null`
    if [ $? -ne 0 ];then
        echo FATAL error: Install virtualenv failed.
        exit 1
    fi
fi


echo defalut path: ~/python_env/appium
venv_path=~/python_env/appium
if [ ! -d "$venv_path" ];then
    mkdir ${venv_path}
    cmd=`python -m venv ${venv_path} 2>/dev/null`
    if [ $? -ne 0 ];then
        echo FATAL error: Create python venv failed.
        exit 1
    fi
    echo Create python venv success.
fi

# ========================Install Appium-Python-Client===============================
cmd=`source ${venv_path}/bin/activate 2>/dev/null`
if [ $? -ne 0 ];then
    echo FATAL error: Enter python venv failed.
    exit 1
fi

cmd=`pip show Appium-Python-Client 2>/dev/null`
if [ $? -ne 0 ];then
    cmd=`pip install Appium-Python-Client 2>/dev/null`
    if [ $? -ne 0 ];then
        echo FATAL error: Install Appium-Python-Client failed.
        cmd=`deactivate 2>/dev/null`
        exit 1
    fi
fi

cmd=`pip show pytest 2>/dev/null`
if [ $? -ne 0 ];then
    cmd=`pip install pytest 2>/dev/null`
    if [ $? -ne 0 ];then
        echo Warning: Install pytest failed.
    fi
fi
cmd=`deactivate 2>/dev/null`
echo ========================Install Appium-Python-Client sucess===============================


echo ========================Appium Server Install===============================
jdk_home=~/program/jdk
program_install_home=~/program
if [ ! -d "$jdk_home" ];then
    echo JDK download....
    cmd=`wget -O /tmp/jdk.tar.gz https://download.oracle.com/java/20/latest/jdk-20_linux-x64_bin.tar.gz 2>/dev/null`
    if [ $? -ne 0 ];then
        echo FATAL error: download jdk failed.
        exit 1
    fi
    tar zxf /tmp/jdk.tar.gz ${program_install_home}
    mv ${program_install_home}/jdk-20.0.2 ${jdk_home}
fi
echo Install jdk success.

if [[ "$JAVA_HOME" == "" ]];then
    echo 'export JAVA_HOME=~/program/jdk'>> ~/.bashrc
    echo 'export PATH="$JAVA_HOME/bin:$PATH"'>> ~/.bashrc
    source ~/.bashrc
    echo Install JAVA_HOME ENN to PATH success.
fi

android_home=~/program/android_sdk
if [ ! -d "$android_home" ];then
    echo Android SDK Tools download....
    cmd=`wget -O /tmp/android_sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-9477386_latest.zip 2>/dev/null`
    if [ $? -ne 0 ];then
        echo FATAL error: download android sdk tools failed.
        exit 1
    fi
    mkdir -p ${android_home}
    unzip /tmp/android_sdk.zip -d ${android_home}
    mkdir -p ${android_home}/cmdline-tools/latest
    mv ${android_home}/cmdline-tools/bin ${android_home}/cmdline-tools/lib ${android_home}/cmdline-tools/NOTICE.txt ${android_home}/cmdline-tools/source.properties ${android_home}/cmdline-tools/latest/
fi
echo Install android sdk tools success.
if [[ "$" == "" ]];then
    echo 'export ANDROID_HOME=~/program/android_sdk'>> ~/.bashrc
    echo 'export PATH="$ANDROID_HOME:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$PATH"'>> ~/.bashrc
    source ~/.bashrc
    echo Install ANDROID_HOME ENN to PATH success.
fi

echo =====Android SDK Tools download "tools"....
echo y | ${android_home}/cmdline-tools/latest/bin/sdkmanager "tools"

echo =====Android SDK Tools download "platform-tools"....
${android_home}/cmdline-tools/latest/bin/sdkmanager "platform-tools"

echo =====Android SDK Tools download "build-tools;33.0.1" "platforms;android-33"....
${android_home}/cmdline-tools/latest/bin/sdkmanager "build-tools;33.0.1" "platforms;android-33"

echo =====Android SDK Tools download "system-images;android-33;google_apis;x86_64"....
echo y | ${android_home}/cmdline-tools/latest/bin/sdkmanager "system-images;android-33;google_apis;x86_64"



echo =====Install nodejs/npm=============
cmd=`node -v 2>/dev/null`
if [ $? -ne 0 ];then
    cmd=`sudo apt install node 2>/dev/null`
    if [ $? -ne 0 ];then
        echo FATAL error: Install nodejs failed.
        exit 1
    fi
fi
echo =====Install nodejs/npm success============

echo =====Install Appium Server=============
cmd=`appium -v 2>/dev/null`
if [ $? -ne 0 ];then
    cmd=`sudo npm i -g appium 2>/dev/null`
    if [ $? -ne 0 ];then
        echo FATAL error: Install appium server failed.
        exit 1
    fi
fi

echo =====Install Appium Driver uiautomator2=============
cmd=`appium driver install uiautomator2 2>/dev/null`

echo =====Create avd named "testPhone"=============
echo no | ${android_home}/cmdline-tools/latest/bin/avdmanager create avd -n "testPhone" -k "system-images;android-33;google_apis;x86_64"


echo ========================Install Done!!!===============================